<?
class Search extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('encrypt');
	}

	function index()
	{
		date_default_timezone_set('Asia/Bangkok');
			$config = $this->config->item('db_config');
			$this->load->model('Dbmodel', '', $config);


	

			$from_date = strtotime($this->Dbmodel->converFormatDate($this->security->xss_clean($this->input->post('from_date'))));
			$search_hotel = $this->security->xss_clean($this->input->post('search_hotel'));
			$to_date = strtotime($this->Dbmodel->converFormatDate($this->security->xss_clean($this->input->post('to_date'))));
			$keyword = $this->security->xss_clean($this->input->post('keyword'));

	

	//        echo $from_date."<BR>";
	//		echo $to_date."<BR>";
//			die;

			if ( $search_hotel == 'Check it')
			{
					

					$data['info_keyword'] = array(
							'keyword' => $keyword ,
							'date_start' => $this->input->post('from_date') ,
							'date_end' => $this->input->post('to_date') ,
						);

			

	//				$objHotel = $this->Dbmodel->select_like('hotel', array('hotel_city' => $keyword), array('hotel_description' => $keyword), "", 
	//					"room_type, room_type.hotel_id = hotel.hotel_id", "hotel.hotel_id asc");

//					$objHotel = $this->Dbmodel->multi_join('hotel', '', "room_type:room_type.hotel_id = hotel.hotel_id,booking:booking.rt_id=room_type.rt_id", "", "hotel.hotel_id asc", array('hotel_city' => $keyword), array('hotel_location' => $keyword));

					$con = mysql_connect("localhost","root","123");
					mysql_select_db("bookinghotel") or die(mysql_error());

					$sql = "SELECT * 
							FROM hotel, room_type, booking
							WHERE hotel.hotel_id = room_type.hotel_id
							AND booking.rt_id = room_type.rt_id
							AND (
							booking_date_start >$to_date
							OR booking_date_end <$from_date
							)";

					$sql = "SELECT room_type.rt_id as rtid, hotel.*, booking.*,room_type.*
								FROM hotel, room_type
								LEFT JOIN booking ON booking.rt_id = room_type.rt_id
								WHERE hotel.hotel_id = room_type.hotel_id
								AND (
								hotel.hotel_city LIKE '%$keyword%'
								OR hotel.hotel_location LIKE '%$keyword%'
								)";

//booking_date_start < $from_date     booking_date_end > $to_date
					$res = mysql_query($sql);

					$i = 0;

					$data['hotel_avaliable'] = '';
					while($row = mysql_fetch_array($res))
					{
						if ( ($row['booking_date_start'] > $to_date || $row['booking_date_end'] < $from_date) || $row['booking_id'] == NULL )
						{
								$data['hotel_avaliable'][$i] = array(
										'hotel_id' => $row['hotel_id'] ,
										'hotel_name' => $row['hotel_name'] ,
										'hotel_city' => $row['hotel_city'] ,
										'hotel_description' => $row['hotel_description'] ,
										'hotel_location' => $row['hotel_location'] ,
										'hotel_address' => $row['hotel_address'] ,
										'hotel_hint' => $row['hotel_hint'] ,
										'mem_id' => $row['mem_id'] ,
										'rt_id' => $row['rtid'] ,
										'rt_name' => $row['rt_name'] ,
										'rt_description' => $row['rt_description'] ,
										'rt_quantity' => $row['rt_quantity'] ,
										'rt_price' => $row['rt_price'] ,
										'client_id' => $row['client_id'] ,
									
									);
								$i++;
						}
					}
				
	//				print_r($data['hotel_avaliable']);
	//				die;

					$this->load->view('search_hotelview', $data);


				

			}

	}  // end function index()

	function room($hotel_id)
	{
		
	}

}

?>